package service

import (
	"fmt"
	"os"

	"gitee.com/jabberwocky238/proj-backend/util"
	"github.com/google/uuid"
)

func debugmusicbuilder(title string) *DebugMusic {
	return &DebugMusic{
		uuid:  uuid.New().String(),
		title: title,
	}
}

var playlists = []struct {
	uuid   string
	title  string
	musics []IMusic
}{
	{
		uuid:  "213452162728931",
		title: "playlist1",
		musics: []IMusic{
			debugmusicbuilder("Give It To Me x Say It Right TikTok Remix"),
			debugmusicbuilder("I like the way you kiss me x After Dark TikTok Remix"),
			debugmusicbuilder("I m Good Blue x Stereo Love sped up reverb"),
			debugmusicbuilder("killshot- magdalena bay (slowed + reverbed)"),
			debugmusicbuilder("Kreayshawn - Missing Kitty (speed upreverb)"),
		},
	},
	{
		uuid:  "213452162728932",
		title: "playlist2",
		musics: []IMusic{
			debugmusicbuilder("Lace and Leather (eternal2dlife slowed) - Britney Spears"),
			debugmusicbuilder("LOWX - END ~ (SUPER SLOWED + REVERB) [BRAZILIAN PHONK] - 2160"),
			debugmusicbuilder("Memory Reboot x After Dark TikTok Remix TRENDING"),
			debugmusicbuilder("Mother Mother - Hayloft (Slowedreve8d)"),
			debugmusicbuilder("Narvent, KoruSe - Anxiety (slowed + reverb) - 1080"),
			debugmusicbuilder("Nasty x Space Song Nightcore"),
			debugmusicbuilder("poker face - lady gaga (slowed + reverbed)"),
			debugmusicbuilder("Renegade by Aaryan Shah - with Rain - (slowed + reverb) - 2160"),
			debugmusicbuilder("Roi - Videoclub (instrumental) Super Slowed [Chainsaw Man Girls]"),
			debugmusicbuilder("Say It Right (TikTok Remix) - 2160"),
			debugmusicbuilder("siouxxie - Masquerade (slowedreverb)"),
			debugmusicbuilder("The Hills x Say It Right TikTok Remix"),
			debugmusicbuilder("Too Sweet x I Follow Rivers slowed to perfection"),
			debugmusicbuilder("Unwritten x Midnight City TikTok Remix"),
			debugmusicbuilder("Why Mona - Wannabe -- slowed + reverb @why mona"),
			debugmusicbuilder("wings dont make you fly"),
			debugmusicbuilder("XXANTERİA - Bombo (Slowed + Reverb) (Krushfunk)"),
			debugmusicbuilder("Yad (Яд) (sped up to perfection) (TikTok Remix)"),
		},
	},
	{
		uuid:  "213452162728933",
		title: "playlist3",
		musics: []IMusic{
			debugmusicbuilder("caravan palace (slowed + reverb) - lone digger"),
			debugmusicbuilder("Cудно (Slowed + Reverb) - Молчат Дома"),
			debugmusicbuilder("Don t Stop The Music x I Was Never There slowed reverb"),
			debugmusicbuilder("Evanescence - Bring Me To Life (speed up)"),
		},
	},
}

type DebugMusic struct {
	uuid  string
	title string
}

func (m *DebugMusic) ID() string {
	return m.uuid
}

func (m *DebugMusic) Title() string {
	return m.title
}

func (m *DebugMusic) Url() (string, error) {
	var url string
	for _, p := range playlists {
		for _, mm := range p.musics {
			if mm.ID() == m.ID() {
				url = fmt.Sprintf("http://localhost:9900/blobs/%s.mp3", mm.Title())
				return util.URLencode(url), nil
			}
		}
	}
	return "", fmt.Errorf("music not found")
}

func (m *DebugMusic) Artist() string {
	return "me"
}

func (m *DebugMusic) Uploader() string {
	return "me"
}

func (m *DebugMusic) CoverUrl() string {
	// os.Lstat 检查路径是否存在
	exts := []string{"png", "jpg", "PNG"}
	for _, ext := range exts {
		path := fmt.Sprintf("E:/1-code/project/proj-blob/cover/%s.%s", m.title, ext)
		_, err := os.Lstat(path)
		if err == nil {
			url := fmt.Sprintf("http://localhost:9900/cover/%s.%s", m.title, ext)
			return util.URLencode(url)
		}
		continue
	}
	return "http://localhost:9900/cover/default.jpg"
}

type DebugPlaylist struct {
	uuid string
}

func (d *DebugPlaylist) ID() string {
	return d.uuid
}

func (d *DebugPlaylist) Title() string {
	for _, p := range playlists {
		if p.uuid == d.uuid {
			return p.title
		}
	}
	return ""
}

// Musics implements IPlaylist.
func (d *DebugPlaylist) Musics() []IMusic {
	for _, p := range playlists {
		if p.uuid == d.uuid {
			return p.musics
		}
	}
	return nil
}

// addMusic implements IPlaylist.
func (d *DebugPlaylist) addMusic(music IMusic) error {
	panic("unimplemented")
}

// checkMusicExist implements IPlaylist.
func (d *DebugPlaylist) checkMusicExist(music IMusic) (bool, error) {
	panic("unimplemented")
}

// removeMusic implements IPlaylist.
func (d *DebugPlaylist) removeMusic(music IMusic) error {
	panic("unimplemented")
}
